package net.samsb.request;

import android.app.ProgressDialog;
import android.util.Log;

import net.samsb.BaseApplication;
import net.samsb.log.LogUtils;
import net.samsb.response.WebSocketResponseListener;

import okhttp3.Request;

import static net.samsb.activity.Constants.URL_COOKIES;
import static net.samsb.activity.Constants.URL_DOMAIN_NAME;
import static net.samsb.activity.Constants.URL_PORT;

/**
 * Created by Destiny_hao on 2018/3/24.
 */

public class WebSocketRequest {

    public void getWebSocketMessage(ProgressDialog progressDialog) {

        String url = null;

        if ( URL_DOMAIN_NAME.indexOf(":") > 1 ) {
            url = "ws://" + URL_DOMAIN_NAME + "/websocket/" + URL_COOKIES;
        }

        else {
            url = "ws://" + URL_DOMAIN_NAME + ":" + URL_PORT +  "/websocket/" + URL_COOKIES;
        }

        if (progressDialog == null) {
            LogUtils.e("getWebSocketMessage", "重连：" + url);
        }

        final Request request = new Request.Builder()
                .url(url)
                .build();

        BaseApplication.mOkHttpClient
                .newWebSocket(
                        request,
                        new WebSocketResponseListener(progressDialog)
                );
    }

    public void getWebSocketMessage(String port, ProgressDialog progressDialog) {

        final Request request;

        if (port == null || "".equals(port)) {
            request = new Request.Builder()
                    .url("ws://" + URL_DOMAIN_NAME + "/websocket/" + URL_COOKIES)
                    .build();
        } else {
            request = new Request.Builder()
                    .url("ws://" + URL_DOMAIN_NAME + ":" + port + "/websocket/" + URL_COOKIES)
                    .build();
        }

        BaseApplication.mOkHttpClient
                .newWebSocket(
                        request,
                        new WebSocketResponseListener(progressDialog)
                );
    }

    public void getWebSocketMessage (String domain, String port, String cookies, ProgressDialog progressDialog) {

        String url = null;

        if (domain.indexOf(":") > 1) {
            url = "ws://" + domain + "/websocket/" + cookies;
        }
        else {
            url = "ws://" + domain + ":" + port + "/websocket/" + cookies;
        }

        LogUtils.e("getWebSocketMessage", url);

        final Request request = new Request.Builder()
                .url(url)
                .build();

        BaseApplication.mOkHttpClient
                .newWebSocket(
                        request,
                        new WebSocketResponseListener("test", progressDialog)
                );
    }

}
